/**
 * @author Qi
 */
var ecsMgr = {
	commentsData:null,
	initUserList:function(selecterId){
		dojo.xhrGet({
			url:"getAllUsers.jsp",
			handleAs:"json",
			load:function(feedback){
				for(var i=0;i<feedback.length;i++){
					var option = new Option(feedback[i].name,feedback[i].id);
					dojo.byId(selecterId).add(option,null);
				}
				//select the last
				dojo.byId(selecterId).selectedIndex = feedback.length;
				var userId = dojo.byId(selecterId).options[feedback.length].value;
				ecsMgr.getCommentsFor(userId);
			}
		})
	},
	getCommentsFor:function(userId){
		dojo.xhrGet({
			url:"getCommentsOfSingleUser.jsp?id="+userId,
			handleAs:"json",
			load:function(feedback){
				ecsMgr.commentsData = feedback;
				dojo.attr(dojo.byId("bkListLink"),"commentCounts",feedback.length);
				dojo.attr(dojo.byId("bkdetailsLink"),"commentCounts",feedback.length);
				var pagingItems = dojo.query("li.lotusFirst");
				pagingItems[0].innerHTML = (feedback.length>0)?1:0;
				pagingItems[1].innerHTML = feedback.length;
				pagingItems[2].innerHTML = feedback.length;
				//ecsMgr.renderAllComments(feedback);
				ecsMgr.sortByDesc();
			}
		})
	},
	_compareCommentsDesc:function(comment1,comment2){
		var repliesCounts1 = comment1.replies.length;
		var repliesCounts2 = comment2.replies.length;
		if(repliesCounts1 < repliesCounts2)
		    return 1;
		else if(repliesCounts1 > repliesCounts2)
		    return -1;
		else
			return 0;
	},
	_compareCommentsAsc:function(comment1,comment2){
		return -ecsMgr._compareCommentsDesc(comment1,comment2);
	},
	sortByDesc:function(){
		ecsMgr.commentsData.sort(ecsMgr._compareCommentsDesc);
		ecsMgr.renderAllComments(ecsMgr.commentsData);
	},
	sortByAsc:function(){
		ecsMgr.commentsData.sort(ecsMgr._compareCommentsAsc);
		ecsMgr.renderAllComments(ecsMgr.commentsData);
	},
	renderAllComments:function(comments){
		for(var i = 0;i < comments.length;i++){
			this.render(comments[i],i);
		}
	},
	render:function(comment,index){
		var commentInfo = dojo.clone(dojo.byId("commentInfo"));
		dojo.attr(commentInfo,"id","commentInfo_"+index);
		var commentImg = dojo.query(".commentImg",commentInfo)[0];
		dojo.attr(commentImg,"title",comment.title);
		dojo.attr(commentImg,"alt",comment.url);
		
		var commentTitle = dojo.query(".commentTitle",commentInfo)[0];
		
		dojo.attr(commentTitle,"href",comment.url);
		dojo.attr(commentTitle,"title",comment.title);
		commentTitle.innerHTML = unescape(comment.title);
		var showDetailsAction = dojo.query(".lotusAction",commentInfo)[0];
		dojo.attr(showDetailsAction,"id","showMore_"+index);
		var hideDetailsAction = dojo.query(".lotusAction",commentInfo)[1];
		dojo.attr(hideDetailsAction,"id","hideMore_"+index);
		dojo.attr(showDetailsAction,"commentIndex",index);
		dojo.attr(hideDetailsAction,"commentIndex",index);
		var replyCard = dojo.query(".commentReplies",commentInfo)[0];
		dojo.attr(replyCard,"id","replyCard_"+index);
		dojo.attr(replyCard,"commentIndex",index);
		replyCard.innerHTML = comment.replies.length;
		
		var commentDetails = dojo.clone(dojo.byId("commentDetails"));
		dojo.attr(commentDetails,"id","details_"+index);
		dojo.query(".commentContent",commentDetails)[0].innerHTML = unescape(comment.comment);
		
		var commentAction = dojo.clone(dojo.byId("commentAction"));
		dojo.attr(commentAction,"id","editAction_"+index);
		var commentContent = dojo.query(".lotusText",commentAction)[0];
	    commentContent.innerHTML = comment.comment;
		
		if(comment.replies.length > 0){
			dojo.attr(commentImg,"src","css/repliedComment.ico");
			var replyList = dojo.query(".replies",commentInfo)[0];
			dojo.attr(replyList,"id","replies_"+index);
			for(var i = 0;i < comment.replies.length;i++){
				var item = document.createElement("li");
				item.innerHTML = comment.replies[i].reply;
				replyList.appendChild(item);
			}
		}else
		    dojo.attr(commentImg,"src","css/comment.ico");
		
		var commentsTable = dojo.byId("commentsTable");
		dojo.style(commentInfo,"display","block");
		commentsTable.appendChild(commentInfo);
		commentsTable.appendChild(commentDetails);
		commentsTable.appendChild(commentAction);
	}
}
function modifyUserId(){
	var userId = dojo.byId("userSelector").options[dojo.byId("userSelector").selectedIndex].value;
	clear();
    ecsMgr.getCommentsFor(userId)
}
function clear(){
	var commentsNodes = dojo.byId("commentsTable").childNodes;
	for(var i = commentsNodes.length-1;i>6;i--){
		dojo.byId("commentsTable").removeChild(commentsNodes[i])
	}
}
function showDetails(index){
    dojo.byId('details_' + index).className = 'lotusDetails';
    dojo.byId('showMore_' + index).style.display = 'none';
    dojo.byId('hideMore_' + index).style.display = '';
}

function hideDetails(index){
    dojo.byId('details_' + index).className = 'lotusHidden lotusDetails';
    dojo.byId('showMore_' + index).style.display = '';
    dojo.byId('hideMore_' + index).style.display = 'none';
}
function showDetailsAll(count){
    for (var i = 0; i <= count; i++) {
        showDetails(i);
    }
    if (dojo.byId('bkListLink')) 
        dojo.byId('bkListLink').className = 'lotusSprite lotusView lotusSummaryOff';
    if (dojo.byId('bkdetailsLink')) 
        dojo.byId('bkdetailsLink').className = 'lotusSprite lotusView lotusDetailsOn';
    //setCookie4View(true);
}

function hideDetailsAll(count){
    for (var i = 0; i <= count; i++) {
        hideDetails(i);
    }
    dojo.byId('bkListLink').className = 'lotusSprite lotusView lotusSummaryOn';
    dojo.byId('bkdetailsLink').className = 'lotusSprite lotusView lotusDetailsOff';
    //setCookie4View(false);
}
function showReplies(e){
	var index = e.currentTarget.getAttribute("commentIndex");
	dojo.style(dojo.byId("replies_"+index),"display","block");
	dojo.byId("replyCard_"+index).onclick = hideReplies;
}
function hideReplies(e){
	var index = e.currentTarget.getAttribute("commentIndex");
	dojo.style(dojo.byId("replies_"+index),"display","none");
	dojo.byId("replyCard_"+index).onclick = showReplies;
}
function sortByRepliesDesc(){
	var sortLink = dojo.byId("sortLink");
    sortLink.className = "lotusDescending lotusActiveSort";
	sortLink.onclick = sortByRepliesAsc;
	clear();
	ecsMgr.sortByDesc();
}
function sortByRepliesAsc(){
	var sortLink = dojo.byId("sortLink");
    sortLink.className = "lotusAscending lotusActiveSort";
	sortLink.onclick = sortByRepliesDesc;
	clear();
	ecsMgr.sortByAsc();
} 